export function uploadImage(context, filename) {
  return new Promise(resolve => {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({ title: '上传中', })
        const filePath = res.tempFilePaths[0]
        const cloudPath = filename + filePath.match(/\.[^.]+?$/)[0]

        // 上传图片
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => { resolve(res) },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => { console.error(e) }
    })
  })
}